package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/servicerpc/servicerpc"
)

type Article struct {
	gorm.Model
	Title   string `gorm:"type:varchar(50);not null;comment:'稿件标题'"`
	Content string `gorm:"type:varchar(50);not null;comment:'稿件内容'"`
	Author  string `gorm:"type:varchar(50);not null;comment:'稿件作者'"`
	ClassId int64  `gorm:"type:int(11);not null;comment:'稿件分类id'"`
}

func (a *Article) ArticlesAdd(db *gorm.DB) error {
	return db.Debug().Create(a).Error
}

func (a *Article) ArticlesFindByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title = ?", title).First(a).Error
}

func (a *Article) ArticleShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id =?", id).First(a).Error
}

func ArticleList(db *gorm.DB, lists []*servicerpc.ArticleList) []*servicerpc.ArticleList {
	db.Debug().Model(&Article{}).Select("articles.id as article_id",
		"articles.title",
		"articles.content",
		"articles.author",
		"classes.name as class_name",
		"articles.created_at as created_at").
		Joins("LEFT JOIN classes on articles.class_id=classes.id").Find(&lists)

	return lists

}
